import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { fang_house } from '../api/house';
import { fang_house_sand } from '@/api/house';
import { fang_second_house } from '@/api/house';
const IndexModel = {
  namespace: 'index',

  state: {
    fang_house: JSON.parse(localStorage.getItem('fang_house')) || [],
    fang_house_sand: [],
    fang_second_house: [],
  },

  effects: {
    *INIT({ payload }, { call, put }) {
      const data = yield call(fang_house);

      yield put({ type: 'fang_house', payload: data.data.RECORDS });
    },
    *sand_LIST({ payload }, { call, put }) {
      const data = yield call(fang_house_sand);

      yield put({ type: 'Fang_house_sand', payload: data.data.RECORDS });
    },
    *second_house({ payload }, { call, put }) {
      const data = yield call(fang_second_house);

      yield put({ type: 'Fang_second_house', payload: data.data.RECORDS });
    },
  },
  reducers: {
    fang_house(state, { payload }) {
      // console.log(state, payload, 'sss');
      state.fang_house = payload;
      //持久化
      localStorage.setItem('fang_house', JSON.stringify(state.fang_house));
      return {
        ...state,
      };
    },
    DEL_DATA(state, { payload }) {
      state.fang_house.forEach((item, index) => {
        if (item.id === payload.id) {
          state.fang_house.splice(index, 1);
        }
      });
      state.fang_house = [...state.fang_house];
      //持久化
      localStorage.setItem('fang_house', JSON.stringify(state.fang_house));
      return {
        ...state,
      };
    },
    EDIT_List(state, { payload }) {
      state.fang_house.forEach((item, index) => {
        if (item.id === payload.id) {
          state.fang_house[index] = payload;
        }
      });
      console.log(state.fang_house, 'fang_house');
      state.fang_house = [...state.fang_house];
      //持久化
      localStorage.setItem('fang_house', JSON.stringify(state.fang_house));
      return {
        ...state,
        // ...payload,
      };
    },
    //沙盘
    Fang_house_sand(state, { payload }) {
      state.fang_house_sand = payload;

      return {
        ...state,
      };
    },
    //沙盘删除
    house_sand_DEL(state, { payload }) {
      state.fang_house_sand.forEach((item, index) => {
        if (item.id === payload.id) {
          state.fang_house_sand.splice(index, 1);
        }
      });
      state.fang_house_sand = [...state.fang_house_sand];
      return {
        ...state,
      };
    },
    //二手房的数据
    Fang_second_house(state, { payload }) {
      state.fang_second_house = payload;
      return {
        ...state,
      };
    },
    //搜索
    second_Seach(state, { payload }) {
      console.log('====================================');
      console.log(payload);
      console.log('====================================');

      state.fang_second_house = state.fang_second_house.filter((item) =>
        item.title.includes(payload),
      );

      state.fang_second_house = [...state.fang_second_house];
      return {
        ...state,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // }, 
  },
  subscriptions: {
    setup({ dispatch, history }) {
      dispatch({ type: 'INIT' });
    },
  },
};

export default IndexModel;
